
import os
import json
import pandas as pd

os.makedirs('temp', exist_ok=True)

def clean_str_optimized(str1, 
    replace_dict = {
        '（': '(',
        '）': ')',
        '、': '/',
        '，': ',',
        ' ': '',
        '；':',',
        ';':',',
    }
    ):

    # 创建转换表
    trans_table = str1.maketrans(replace_dict)
    return str1.translate(trans_table)

import re

def split_text(input_text):
    # 使用正则表达式分别匹配英文和中文部分
    english_part = re.findall(r'[A-Za-z]+', input_text)
    chinese_part = re.findall(r'[\u4e00-\u9fff，]+', input_text)

    # 如果没有匹配到则返回空字符串
    english = english_part[0] if english_part else ''
    chinese = chinese_part[0] if chinese_part else ''

    return [english, chinese]

# # 测试函数
# test_input = "field田地,农田"
# split_text(test_input)


def load_one(dir_name='input', name='CET4'):
    p = f'{dir_name}/{name}.json'
    word_list = json.load(open(p, 'rb'))

    rows = []
    for d in word_list:
        word = d.get('word', '')
        trans_str = ''
        simple_str = ''
        phrases_str = ''
        type_list = []
        for j, d1 in enumerate(d.get('translations', [])):
            a_j =  d1.get('translation', '')
            b_j = d1.get('type', '')
            a_j = clean_str_optimized(a_j)
            if j==0:
                simple_str += a_j.split(',')[0]

            trans_str += b_j+'.'+a_j

            if b_j:
                type_list.append(b_j)

        for j, d2 in enumerate(d.get('phrases', [])):
            a_j = d2.get('translation', '')
            b_j = d2.get('phrase', '')
            a_j = clean_str_optimized(a_j)
            phrases_str += a_j+b_j+'  '
        type_list = [x.replace('&', ',').replace(' ', '') for x in type_list]
        dict_i = {
            'word':word.lower(),
            'simple_str':simple_str,
            'trans_str':trans_str,
            'phrases_str':phrases_str,
            'type_list':'.'.join(type_list)+'.',
            'level':name,
        }
        rows.append(dict_i)

    df = pd.DataFrame(rows)
    # df.to_excel('test.xlsx', index=False)
    return df

def create_all_word():
    dir_name = 'input/单词json'
    name_list = [x.replace('.json', '') for x in os.listdir(dir_name) if '.json' in x]
    for i,name in enumerate(name_list):
        # print(i, '..........', name)
        if i==0:
            df = load_one(dir_name=dir_name, name=name)
        else:
            df = df.append(load_one(dir_name=dir_name, name=name))
    study_turn_dict = {
        '初中':1,
        '高中':2,
        'CET4':3,
        'CET6':4,
        '考研':5,
        '托福':6,
        'SAT':7,
    }
    df['study_turn'] = df['level'].map(study_turn_dict)
    df.sort_values('study_turn', inplace=True)
    df.drop_duplicates(subset=['word'], inplace=True)
    df = df.sort_values(['word'])
    df = df.reset_index(drop=True)
    df.to_excel('temp/a01_all_word.xlsx', index=False)


def create_format1_ext(p='input/词根词缀/format1.txt'):
    lines = open(p, 'r', encoding='utf-8').readlines()
    lines = [x.replace('\n', '') for x in lines]

    rows = []
    for line in lines[:300]:
        replace_dict = {
            '（': '(',
            '）': ')',
            '、': ',',
            '，': ',',
            ' ': '',
            '；':',',
            ';':',',
            '/':',',
        }
        line = clean_str_optimized(line, replace_dict)
        if '=' in line:
            ext_list = line.split('=')[0].replace('-','').split(',')
            en_cn_list = split_text(line.split('=')[1].replace('表示',''))
            # print(en_cn_list,'==>', ext_list)
        elif '表示' in line:
            line = line.replace('词根','').replace('-','')  # .replace('-','')

            ext_list = line.split('表示')[0].replace('-','').split(',')
            en_cn_list = split_text(line.split('表示')[1])
            # print(en_cn_list,'==>', ext_list)
        else:
            line = line.replace('-','')  # .replace('-','')
            en_cn_list = split_text(line)
            if len(en_cn_list)==2:
                ext_list = en_cn_list[0].replace('-','').split(',')
                en_cn_list = split_text(en_cn_list[1])
                # print(en_cn_list,'==>', ext_list)
            else:
                print('检查......................................', line)
            pass
        # print(en_cn_list,'==>', ext_list)
        for ext in ext_list:
            dict_i = {
                'ext':ext,
                'ext_len':len(ext),
                'en':en_cn_list[0],
                'cn':en_cn_list[1]
            }
            rows.append(dict_i)
    df = pd.DataFrame(rows)
    df = df[df['ext_len']>1]
    df = df.sort_values('ext_len', ascending=False).reset_index(drop=True)
    df.to_excel('temp/ext.xlsx', index=False)
    pass



def run():
    # load_one()
    create_all_word()
    # create_format1_ext()
    pass


if __name__=='__main__':
    run()